# -*- coding: utf-8 -*-

# 获取客户端引擎API模块
import mod.client.extraClientApi as clientApi

# 获取客户端system的基类ClientSystem
ClientSystem = clientApi.GetClientSystemCls()


class TutorialClientSystem(ClientSystem):


    ticks_cli = 0
    ticks_cli_10 = 0

    zdjp = False
    xsdw = False

    teamtext = ""

    title = "§c§l开发者致玩家的一封信"
    context = ""


    # 客户端System的初始化函数
    def __init__(self, namespace, systemName):
        # 首先初始化TutorialClientSystem的基类ClientSystem
        super(TutorialClientSystem, self).__init__(namespace, systemName)
        print "==== TutorialClientSystem Init ===="
        print "【玩家ID】" + clientApi.GetLocalPlayerId()
        self.ListenEvent()

    # 监听函数，用于定义和监听函数。函数名称除了强调的其他都是自取的，这个函数也是。
    def ListenEvent(self):
        self.ListenForEvent(clientApi.GetEngineNamespace(),
                            clientApi.GetEngineSystemName(), "OnLocalPlayerStopLoading", self,
                            self.OnLocalPlayerStopLoading)

        self.ListenForEvent(clientApi.GetEngineNamespace(),
                            clientApi.GetEngineSystemName(), "PlayerTryDropItemClientEvent", self,
                            self.OnPlayerTryDropItemClientEvent)

        self.ListenForEvent(clientApi.GetEngineNamespace(),
                            clientApi.GetEngineSystemName(), "PlayerTryDestroyBlockClientEvent", self,
                            self.PlayerTryDestroyBlockClientEvent)

        self.ListenForEvent(clientApi.GetEngineNamespace(),
                            clientApi.GetEngineSystemName(), "OnScriptTickClient", self,
                            self.OnScriptTickClient)

        self.ListenForEvent(clientApi.GetEngineNamespace(),
                            clientApi.GetEngineSystemName(), "OnKeyPressInGame", self,
                            self.OnKeyPressInGame)

        self.ListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'UiInitFinished', self,
                            self.UiInitFinished)

        # self.ListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'data_get', self,
        #                     self.data_get)

        self.ListenForEvent("TutorialMod", "TutorialServerSystem", "data_get", self, self.data_get)

    def GetServerBroadcast(self,args):
        print "==== 获取事件开始 ===="
        print args
        print "==== 获取事件结束 ===="

    def OnLocalPlayerStopLoading(self,args):
        print "===== 出生点地形加载完成时触发 ====="

        # virtualWorldComp = clientApi.GetEngineCompFactory().CreateVirtualWorld(clientApi.GetLevelId())
        # virtualWorldComp.VirtualWorldSetSkyTexture("textures/virtualWorldSky", 0)

    def UiInitFinished(self,g):
        clientApi.RegisterUI("TutorialMod","ui1","tutorialScripts.uiScript.ui1.ui1","ui1.main")
        self.UInode = clientApi.CreateUI("TutorialMod","ui1",{"isHud":1})
        if self.UInode:
            print "UI创建完成"
        else:
            print "UI创建失败"
        buttonUIControl = self.UInode.GetBaseUIControl("/button").asButton()
        buttonUIControl.AddTouchEventParams({"isSwallow":True})
        buttonUIControl.SetButtonTouchUpCallback(self.but)

        buttonUIControl0 = self.UInode.GetBaseUIControl("/button0").asButton()
        buttonUIControl0.AddTouchEventParams({"isSwallow": True})
        buttonUIControl0.SetButtonTouchUpCallback(self.but0)

        buttonUIControl0 = self.UInode.GetBaseUIControl("/shell/background/top/loc/close").asButton()
        buttonUIControl0.AddTouchEventParams({"isSwallow": True})
        buttonUIControl0.SetButtonTouchUpCallback(self.but1_close)

        self.UInode.GetBaseUIControl("/shell/background/top/title").asLabel().SetText(self.title)

    def but(self,g):
        print ("### 自动疾跑")
        if self.zdjp == True:
            # 关闭
            self.zdjp = False
            buttonUIControl = self.UInode.GetBaseUIControl("/button/button_label").asLabel().SetText(
                "自动疾跑：Ctrl+I§c§l（关闭）")

        elif self.zdjp == False:
            # 开启
            self.zdjp = True
            buttonUIControl = self.UInode.GetBaseUIControl("/button/button_label").asLabel().SetText(
                "自动疾跑：Ctrl+I§b§l（开启）")

    def but1_close(self,g):
        lyUIControl1 = self.UInode.GetBaseUIControl("/shell/background").asImage()
        lyUIControl1.SetVisible(False)

    def but0(self,g):
        print ("### 显示队伍")
        if self.xsdw == True:
            # 关闭
            self.xsdw = False
            buttonUIControl = self.UInode.GetBaseUIControl("/button0/button_label").asLabel().SetText(
                "队伍显示：§c§l按住TAB键")
            LabelUIControl = self.UInode.GetBaseUIControl("/label").asLabel()
            LabelUIControl.SetAlpha(0)

        elif self.xsdw == False:
            # 开启
            self.xsdw = True
            buttonUIControl = self.UInode.GetBaseUIControl("/button0/button_label").asLabel().SetText(
                "队伍显示：§3§l按住TAB键")
            LabelUIControl = self.UInode.GetBaseUIControl("/label").asLabel()
            LabelUIControl.SetAlpha(60)


    # 函数名为Destroy才会被调用，在这个System被引擎回收的时候会调这个函数来销毁一些内容
    def Destroy(self):
        print "===== TutorialServerSystem Destroy ====="
        # 调用上面的反监听函数来销毁
        self.UnListenEvent("label","label",)

    def OnPlayerTryDropItemClientEvent(self,args):
        playerId = args['playerId']
        itemDict = args['itemDict']
        newItemName = itemDict['newItemName']


        print "=== itemDict "
        print itemDict

        if newItemName=="minecraft:golden_chestplate":
            args['cancel'] = True
        elif newItemName=="minecraft:redstone":
            args['cancel'] = True
        elif newItemName=="minecraft:barrier":
            args['cancel'] = True
        elif newItemName=="minecraft:enchanted_book":
            args['cancel'] = True
        elif newItemName=="kssae:purple_team_armor_main":
            args['cancel'] = True
        elif newItemName=="kssae:blue_team_armor_main":
            args['cancel'] = True
        elif newItemName=="kssae:green_team_armor_main":
            args['cancel'] = True
        elif newItemName=="kssae:orange_team_armor_main":
            args['cancel'] = True

    can_des = ['minecraft:wool', 'minecraft:ladder', 'minecraft:sandstone', 'minecraft:end_stone',
               'minecraft:nether_brick', 'minecraft:packed_ice', 'minecraft:oak_stairs', 'minecraft:planks',
               'minecraft:emerald_block', 'minecraft:chest', 'minecraft:ender_chest', 'minecraft:slime','minecraft:bed']
    def PlayerTryDestroyBlockClientEvent(self,args):
        x = args['x']
        y = args['y']
        z = args['z']

        blockName = args['blockName']
        playerId = args['playerId']

        sc = False

        for i in self.can_des:
            if blockName == i:
                print "==== 可以破坏blockName"+blockName
                args['cancel'] = False
                sc = True
                print sc
                break

        if sc == False:
            args['cancel'] = True

    def OnScriptTickClient(self):
        self.ticks_cli +=1

        if self.ticks_cli>=30:
            self.ticks_cli = 0
            self.ticks_cli_10 +=1

        if self.ticks_cli_10 >=10:
            self.ticks_cli_10 = 0

            # print ("### OnScriptTickClient_cli")
            # virtualWorldComp = clientApi.GetEngineCompFactory().CreateVirtualWorld(clientApi.GetLevelId())
            # virtualWorldComp.VirtualWorldCreate()
            # # 设置天空为红色
            # virtualWorldComp.VirtualWorldSetSkyTexture("textures/virtualWorldSky", 0)

    def data_get(self,args):
        text = args['text']
        self.teamtext = text
        print ("##clientApi",text)
        LabelUIControl = self.UInode.GetBaseUIControl("/label").asLabel()
        LabelUIControl.SetText(text)

    def OnKeyPressInGame(self,args):
        key = args['key']
        isDown = args['isDown']

        print ("### OnKeyPressInGame",key,isDown)

        if key == "73" and isDown=="1":
            # 键盘

            if self.zdjp == True:
                # 关闭
                self.zdjp = False
                buttonUIControl = self.UInode.GetBaseUIControl("/button/button_label").asLabel().SetText(
                    "自动疾跑：Ctrl+I§c§l（关闭）")

            elif self.zdjp == False:
                # 开启
                self.zdjp = True
                buttonUIControl = self.UInode.GetBaseUIControl("/button/button_label").asLabel().SetText(
                    "自动疾跑：Ctrl+I§b§l（开启）")

        if key == "13" and isDown=="1":
            lyUIControl1 = self.UInode.GetBaseUIControl("/shell/background").asImage()
            lyUIControl1.SetVisible(False)


        print ("### zdjp",self.zdjp)

        if key == "87" and isDown=="1":
            if self.zdjp == True:
                # 开启
                comp = clientApi.GetEngineCompFactory().CreateActorMotion(clientApi.GetLocalPlayerId())
                comp.BeginSprinting()

        if key == "87" and isDown=="0":

            if self.zdjp == True:
                # 关闭
                comp = clientApi.GetEngineCompFactory().CreateActorMotion(clientApi.GetLocalPlayerId())
                comp.EndSprinting()

        if key == "9" and isDown=="1":

            self.xsdw = True
            buttonUIControl = self.UInode.GetBaseUIControl("/button0/button_label").asLabel().SetText(
                "队伍显示：§3§l按住TAB键")

            LabelUIControl = self.UInode.GetBaseUIControl("/label").asLabel()
            LabelUIControl.SetAlpha(60)


        if key == "9" and isDown=="0":

            self.xsdw = False
            buttonUIControl = self.UInode.GetBaseUIControl("/button0/button_label").asLabel().SetText(
                "队伍显示：§c§l按住TAB键")

            LabelUIControl = self.UInode.GetBaseUIControl("/label").asLabel()
            LabelUIControl.SetAlpha(0)



